// **************** LAB4.c *************************

#include <stdio.h>
#include <string.h>
#include "inc/hw_types.h"
#include "rit128x96x4.h"
#include "adc.h"
#include "os.h"
#include "lm3s8962.h"
#include "filter.h"
#include "interpreter.h"
#include "ports.h"

#define SSI_FREQ 1000000
#define LAB4_DEBUG 0
#define TIMESLICE 2*TIME_1MS  // thread switch time in system time units
#define PERIOD TIME_1MS/2     // 2kHz sampling period in system time units

// ***********************************************************************
// *****************   GLOBALS   ***************************************
// *******************************************************************
unsigned long NumCreated;   // number of foreground threads created
unsigned long PIDWork;      // current number of PID calculations finished
unsigned long FilterWork;   // number of digital filter calculations finished
unsigned long NumSamples;   // incremented every sample
unsigned long DataLost;     // data sent by Producer, but not received by Consumer
extern long MaxJitter;    // largest time difference between interrupt trigger and running thread
extern long MinJitter;    // smallest time difference between interrupt trigger and running thread
extern unsigned long const JitterSize;
extern unsigned long JitterHistogram[];
unsigned long Count1 = 0,Count2 =  0,Count3 = 0,Count4 = 0;
Sema4Type OLED;
// ********************************************************************
// **********************************************************************
void Thread1(void){
  while(1){ Count1++; }
}

void Thread2(void){
  while(1){ Count2++; }
}

void Thread3(void){
  while(1){ Count3++; }
}

void Thread4(void){
  while(1){ Count4++; }
}

int main2( void ) {
  Ports_Init_DigOut(); 
  OS_Init();
  OS_InitSemaphore(&OLED,1);
  Filter_Init();
  RIT128x96x4Init(SSI_FREQ);
  RIT128x96x4PlotClear(0,79);
  NumCreated = 0;
  NumCreated += OS_AddThread(&Filter_BP,128,1);
  NumCreated += OS_AddThread(&Interpreter,128,1);
  NumCreated += OS_AddThread(&Filter_Plot,128,1);
  OS_Launch(TIMESLICE);
  return 0;
}

void dummythread(void){
  while(1){}
}

int main(void){
  Ports_Init_DigOut();
  OS_Init();
  OS_InitSemaphore(&OLED,1);
  Filter_Init();
  RIT128x96x4Init(SSI_FREQ);
  RIT128x96x4PlotClear(0,79);

  //NumCreated += OS_AddThread(&dummythread, 128, 1);
  NumCreated += OS_AddThread(&Filter_BP,128,1);
  OS_Launch(TIMESLICE);
  return 0;
}